import fetch from '@system.fetch';
//引入router模块，用户页面跳转
import router from'@system.router'
import app from '@system.app'
import qs from 'querystring';

export default {
    //定义参数
    data: {
        //页面绑定数据
        textColor1: "#00ff00",
        textColor2: "#00ff00",
        bgColor1: "#669966",
        bgColor2: "#669966",
        //默认是第一页
        swiperPage: 0,

        //定义初始值
        airQuality:"health",
        detailData:90,
        percent2: 20,
        //        iconUncheckedColor: '#262626',
        //        iconcheckedColor: '#ffffff',
        //        iconcheckedBR: '6px',
        airData: [
            {
                location: "综合状态评估",
                airQuality: "",
                detailData: 10
            },
            {
                location: "综合状态评估",
                airQuality: "",
                detailData: 90
            }
        ],
        allgrade:[{
                      wen:80,
                      du:90,
                      ping:80,
                      lian:80,
                      xie:90
                  }
        ]
    },
    //跳转到详情页面
    openDetail () {
        this.onInit()
        //        router.replace({
        //            uri: 'pages/detail/detail',
        ////            params: {selectedCityIndex:this.swiperPage}
        //        });
        //发起网络请求
        fetch.fetch({
            url:`http://121.36.102.158:8000/personal_info/appdataRequest3/`,//路径
            //qs.stringify把一个参数对象格式化为一个字符串,将对象 序列化成 URL 的形式，以&进行拼接
            data:qs.stringify({'username':'hi'}),
            //            data:qs.stringify({'datasum':this.detailData}),

            //            data: JSON.stringify({'username':'wink','password':'123'}),
            //验证，转为字符串发给后台
            responseType:"json", //请求的参数类型
            method: "POST",
            success:(resp)=>
            {
                var getdata2
                var a
                var getdata3
                getdata2 = resp.data;  //令获取到的数据赋给percent2

                //将JSON字符串转换为JSON对象(把key=value连接字符串转成对象,如：name=hehe&ange=10==>{name:'hehe', ange:10}
                getdata3 = JSON.parse(resp.data)
                this.detailData=getdata3.avg;
                a = Array(resp.data)
                //                console.log("返回的数据："+this.percent2)//打印出数据
                console.log("返回的数据："+getdata2)//测试：打印出数据
                console.log("返回的数据："+getdata3[0].avg)//正确：打印出数据
                console.log("返回的数据："+getdata3.avg)//测试：打印出数据
                console.log("c:"+typeof(getdata2));
                console.log("c:"+typeof(getdata3));
                console.log("c:"+typeof(a));
                console.log("c:"+resp.data.avg);
                console.log("c:"+resp.data[1]);
                console.log("c:"+a[1]);
                //                this.allgrade[0].wen = getdata2[2]

                this.allgrade[0].wen = getdata3[0].stability
                this.detailData=getdata3[0].avg
                this.percent2=getdata3[0].avg
                this.allgrade[0].du=getdata3[0].independence
                this.allgrade[0].ping=getdata3[0].balance
                this.allgrade[0].lian=getdata3[0].continuity
                this.allgrade[0].xie=getdata3[0].harmony
            },
            fail:(resp)=>
            {
                this.percent2 = resp.data;
                //                console.log("获取数据失败："+this.percent2)
            }
        });
    },
    onInit () {
        //根据数值的不同，设置不同的字体、背景颜色和图片
        //        if(this.airData[0].detailData > 100){
        //            this.src1 = 'common/cloud_red.png';
        //            this.textColor1 = '#ff0000';
        //            this.bgColor1 = '#9d7462';
        //        } else if(50 < this.airData[0].detailData && this.airData[0].detailData <= 100){
        //            this.src1 = 'common/cloud_yellow.png';
        //            this.textColor1 = '#ecf19a';
        //            this.bgColor1 = '#9d9d62';
        //        }
        if(this.airData[1].detailData < 60){
            this.src2 = 'common/cloud_red.png';
            this.textColor2 = '#ff0000';
            this.bgColor2 = '#9d7462';
            this.airData[1].airQuality="terrible";
            //            this.detailData;
        } else if(60 < this.airData[1].detailData && this.airData[1].detailData <= 100){
            this.src2 = 'common/cloud_yellow.png';
            this.textColor2 = '#992bb113';
            this.bgColor2 =  '#9d9d62';
            this.airData[1].airQuality="health";
            //            this.percent2=90
            //            this.detailData;
        }
        if(this.selectedCityIndex){
            this.swiperPage = this.selectedCityIndex;
            if(this.swiperPage == 0){
                this.iconcheckedColor = '#ffffff';
                this.iconUncheckedColor = '#262626';
            }else{
                this.iconcheckedColor = '#262626';
                this.iconUncheckedColor = '#ffffff';
            }
        }
    },
    //退出应用
    exitApp(){
        console.log('start exit');
        app.terminate();
        console.log('end exit');
    },
    //swiper滑动回调事件，保存当前swiper的index值，每次滑动都会将index值保存在swiperPage变量中
    swiperChange (e) {
        this.swiperPage = e.index;
        if(e.index == 0){
            this.iconcheckedColor = '#ffffff';
            this.iconUncheckedColor = '#262626';
        }else{
            this.iconcheckedColor = '#262626';
            this.iconUncheckedColor = '#ffffff';
        }
    },
    launch1() {
        router.push ({
            uri:'pages/dataDetail/dataDetail', // 指定要跳转的页面
        })
    },
    launch2() {
        router.push ({
            uri:'pages/zhuye/zhuye', // 指定要跳转的页面
        })
    },
    clickitem4(){
        router.push({
            uri: 'pages/personalMain/personalMain'
        });
    },
}
